@extends('layouts/index')

@section('title', $default_name)

@section('content')
    <div class="layui-card">
        <div class="layui-card-header notselect">
            <span class="layui-icon layui-icon-next font-s10 color-desc margin-right-5"></span> {{ $default_name }}
            <div class="pull-right">
                @if(array_key_exists('base.user.add',$btns))
                    <button class="layui-btn layui-btn-sm layui-btn-primary" onclick="adduser()">
                        {{ $btns['base.user.add'] }}
                    </button>
                @endif
                @if(array_key_exists('base.user.batch.disable',$btns))
                    <button class="layui-btn layui-btn-sm layui-btn-primary" onclick="batchDisable()">
                        {{ $btns['base.user.batch.disable'] }}
                    </button>
                @endif
                @if(array_key_exists('base.user.batch.recovery',$btns))
                    <button class="layui-btn layui-btn-sm layui-btn-primary" onclick="batchRecovery()">
                        {{ $btns['base.user.batch.recovery'] }}
                    </button>
                @endif
                @if(array_key_exists('base.user.batch.del',$btns))
                    <button class="layui-btn layui-btn-sm layui-btn-primary" onclick="batchRemove()">
                        {{ $btns['base.user.batch.del'] }}
                    </button>
                @endif
            </div>
        </div>
        <div class="layui-card-body">
            <table class="layui-table">
                <caption class="margin-bottom-10 text-left">
                    <fieldset>
                        <form class="layui-form layui-form-pane form-search" method="get" autocomplete="off">
                            <div class="layui-form-item layui-inline">
                                <label class="layui-form-label">{{ trans('user.loginname') }}</label>
                                <div class="layui-input-inline">
                                    <input name="loginname" value="{{ $params['loginname'] }}" id="loginname"
                                           placeholder="{{ trans('user.please_input_loginname') }}"
                                           class="layui-input">
                                </div>
                            </div>
                            <div class="layui-form-item layui-inline">
                                <label class="layui-form-label">{{ trans("user.realname") }}</label>
                                <div class="layui-input-inline">
                                    <input name="realname" value="{{ $params['realname'] }}" id="realname"
                                           placeholder="请输入真实姓名"
                                           class="layui-input">
                                </div>
                            </div>
                            <div class="layui-form-item layui-inline">
                                <label class="layui-form-label">{{ trans("user.phone") }}</label>
                                <div class="layui-input-inline">
                                    <input name="phone" value="{{ $params['phone'] }}" id="phone"
                                           placeholder="{{ trans("user.please_input_phone") }}"
                                           class="layui-input">
                                </div>
                            </div>
                            <div class="layui-form-item layui-inline">
                                <label class="layui-form-label">{{ trans("user.email") }}</label>
                                <div class="layui-input-inline">
                                    <input name="email" value="{{ $params['email'] }}" id="email"
                                           placeholder="{{ trans("user.please_input_email") }}"
                                           class="layui-input">
                                </div>
                            </div>
                            <div class="layui-form-item layui-inline">
                                <button class="layui-btn layui-btn-primary" type="submit"><i class="layui-icon"></i> 搜
                                    索
                                </button>
                                <button class="layui-btn layui-btn-primary" type="button" onclick="resetResult()"><i
                                        class="layui-icon"></i> 重 置
                                </button>
                            </div>
                        </form>
                    </fieldset>
                </caption>

                <thead>
                <tr>
                    <th class='text-left nowrap list-table-check-td think-checkbox'>
                        {{--<label><input type='checkbox'></label>--}}
                    </th>
                    <th class="text-left nowrap" width="2%">ID</th>
                    <th class="text-left nowrap" width="8%">{{ trans("user.loginname") }}</th>
                    <th class="text-left nowrap" width="10%">{{ trans("user.realname") }}</th>
                    <th class="text-left nowrap" width="10%">{{ trans("user.phone") }}</th>
                    <th class="text-left nowrap" width="10%">{{ trans("user.email") }}</th>
                    <th class="text-left nowrap" width="10%">{{ trans("common.create_time") }}</th>
                    <th class="text-left nowrap" width="10%">{{ trans("user.last_login_time") }}</th>
                    <th class="text-left nowrap" width="10%">{{ trans('common.status') }}</th>
                    <th class="text-left nowrap" width="20%">{{ trans('common.operation') }}</th>
                </tr>
                </thead>
                <tbody>
                @foreach($users as $user)
                    <tr data-dbclick="">
                        <td class='list-table-check-td think-checkbox'>
                            @if($user->roles != \App\Models\AdminUsersModel::SUPERUSERS)
                                <label><input class="list-check-box" value='{{ $user->id }}' type='checkbox'></label>
                            @endif
                        </td>
                        <td class="text-left nowrap">
                            {{ $user->id }}
                        </td>
                        <td class="text-left nowrap">
                            {{ $user->loginname }}
                        </td>
                        <td class="text-left nowrap">
                            {{ $user->realname }}
                        </td>
                        <td class="text-left nowrap">
                            {{ $user->phone }}
                        </td>
                        <td class="text-left nowrap">
                            {{ $user->email }}
                        </td>
                        <td class="text-left nowrap">
                            {{ date(trans("common.time_format"),$user->create_time) }}
                        </td>
                        <td class="text-left nowrap">
                            @if($user->last_login_time != 0)
                                {{ date(trans("common.time_format"),$user->last_login_time) }}
                            @endif
                        </td>
                        <td class="text-left nowrap">
                            @if($user->status == 0)
                                <span class="layui-badge layui-bg-green">{{ trans("common.enabled") }}</span>
                            @elseif($user->status==1)
                                <span class="layui-badge">{{ trans("common.disabled") }}</span>
                            @endif
                        </td>
                        <td class="text-left nowrap">
                            @if($user->roles != \App\Models\AdminUsersModel::SUPERUSERS)
                                @if(array_key_exists('base.user.modify',$btns))
                                    <button type="button" class="layui-btn layui-btn-normal layui-btn-xs"
                                            onclick="modifyUser({{ $user->id  }})">{{ $btns['base.user.modify'] }}
                                    </button>
                                @endif

                                @if(array_key_exists('base.user.del',$btns))
                                    <button class="layui-btn layui-btn-danger layui-btn-xs"
                                            onclick="deleteUser({{ $user->id }})">{{ $btns['base.user.del'] }}
                                    </button>
                                @endif

                                @if($user->status == \App\Models\AdminUsersModel::USERSENABLE && array_key_exists('base.user.disable',$btns))
                                    <button class="layui-btn layui-btn-warm layui-btn-xs"
                                            onclick='disableUser({{ $user->id }})'>{{ $btns['base.user.disable'] }}
                                    </button>
                                @endif

                                @if($user->status ==\App\Models\AdminUsersModel::USERSDISABLE && array_key_exists('base.user.enable',$btns))
                                    <button class="layui-btn  layui-btn-xs"
                                            onclick='disableUser({{ $user->id }})'>{{ $btns['base.user.enable'] }}
                                    </button>
                                @endif
                            @endif
                        </td>
                    </tr>
                @endforeach
                </tbody>
            </table>
            <div class="pagination-container nowrap">
                {{ $users->links() }}
            </div>
        </div>
    </div>
    <script type="text/javascript">
        function adduser() {
            layer.open({
                type: 2,
                title: "{{ trans('user.add_user') }}",
                area: ['600px', '480px'],
                content: "{{ url('user/show') }}"
            });
        }

        function batchRemove() {
            var ids = "";
            $.each($(".list-check-box"), function (k, v) {
                if ($(this).prop("checked")) {
                    ids += $(this).val() + ","
                }
            });
            if (ids == "") {
                layer.msg("{{ trans('user.please_select_delete_user') }}");
                return false;
            }
            var index = layer.confirm("{{ trans('user.confirm_delete_user') }}", {
                title: "{{ trans('common.system_info') }}",
                icon: 3,
                anim: 0,
                btn: ["{{ trans('common.sure') }}", "{{ trans('common.cancel') }}"]
            }, function () {
                $.ajax({
                    url: '{{ url("user/del") }}',
                    type: 'POST',
                    dataType: 'json',
                    headers: {
                        'X-CSRF-TOKEN': "{{ csrf_token() }}"
                    },
                    ContentType: "application/json; charset=utf-8",
                    data: {"id": ids},
                    success: function (result) {
                        if (result.code == 0) {
                            layer.msg("{{ trans("common.operation_success") }}", {
                                time: 1000
                            }, function () {
                                window.location.reload();
                            })
                        } else {
                            layer.msg(result.msg)
                        }
                    }
                });
            }, function () {
                layer.close(index);
            });
        }

        function modifyUser(id) {
            layer.open({
                type: 2,
                title: "{{ trans("user.modify_user") }}",
                area: ['600px', '480px'],
                content: '{{ url("user/show") }}?id=' + id
            });
        }

        function deleteUser(id) {
            var index = layer.confirm("{{ trans('user.confirm_delete_user') }}", {
                title: "{{ trans("common.system_info") }}",
                icon: 3,
                anim: 0,
                btn: ["{{ trans("common.sure") }}", "{{ trans("common.cancel") }}"]
            }, function () {
                $.ajax({
                    url: '{{ url("user/del") }}',
                    type: 'POST',
                    dataType: 'json',
                    headers: {
                        'X-CSRF-TOKEN': "{{ csrf_token() }}"
                    },
                    ContentType: "application/json; charset=utf-8",
                    data: {"id": id},
                    success: function (result) {
                        if (result.code == 0) {
                            layer.msg("{{ trans('common.operation_success') }}", {
                                time: 1000
                            }, function () {
                                window.location.reload();
                            })
                        } else {
                            layer.msg(result.msg)
                        }
                    }
                });
            }, function () {
                layer.close(index)
            });
        }

        function disableUser(id) {
            var index = layer.confirm("{{ trans('user.confirm_operation_user') }}", {
                title: "{{ trans("common.system_info") }}",
                icon: 3,
                anim: 0,
                btn: ["{{ trans('common.sure') }}", "{{ trans("common.cancel") }}"]
            }, function () {
                $.ajax({
                    url: '{{ url("user/disable") }}',
                    type: 'POST',
                    dataType: 'json',
                    headers: {
                        'X-CSRF-TOKEN': "{{ csrf_token() }}"
                    },
                    ContentType: "application/json; charset=utf-8",
                    data: {"id": id},
                    success: function (result) {
                        if (result.code == 0) {
                            layer.msg("{{ trans('common.operation_success') }}", {
                                time: 1000
                            }, function () {
                                window.location.reload();
                            })
                        } else {
                            layer.msg(result.msg)
                        }
                    }
                });
            }, function () {
                layer.close(index)
            });
        }


        function resetResult() {
            $("#loginname").val("");
            $("#realname").val("");
            $("#phone").val("");
            $("#email").val("");
            $(".form-search").submit()
        }

    </script>
@endsection

